var CMS={};
CMS.LightBox={};
CMS.LightBox.init=function(selector,group){
	var $select_object = $(selector);
	if($select_object!==undefined){
		$.each($select_object,function(index,element){
			var $a = $("<a>");
			var $element = $(element);
			if($element!==undefined){
				var src = $element.attr("src");
				
				var $p = $element.parent().next();
				var title = undefined;
				if($p!==undefined){
					title = $p.html();
					$a.attr("title",title);
				}
				$a.attr("href",src);
				$a.attr("data-lightbox",group);
				$element.wrap($a);
			}
		});
	}
};

CMS.Products = {};
CMS.Products.gets = function(options,contentContainer,title){
	$(".section-title").text(title);
	var $container = $("#"+contentContainer);
	if($container!==undefined){
		$container.empty();
		$container.append('<span style="color:green">获取产品信息中，请稍等</span>');
	}
	$.get("products_get.jspx?format=json",options,function(data){
		if(data!==undefined){
			CMS.Products.renderProduct($container,data);
		}
	});
};
CMS.Products.search = function(contentContainer){
	
	$('#search-button').on('click',function(){
		var keyword=$('#keyword').val();
		if(keyword.length==0){
			alert('请输入查询关键词');
		}else{
			var $container = $('#'+contentContainer);
			if($container!==undefined){
				$container.empty();
				$container.append('<span style="color:green">检索中，请稍候...</span>');
			}
			$.post('search.jspx?format=json',{keyword:keyword},function(data){
				$(".section-title").text('检索结果');
				if(data!==undefined){
					if(data.hasError){
						$container.empty();
						$container.append('<span sytle="color:red">'+data.message+'</span>');
					}else{
						$(".section-title").text('检索结果('+data.total+')');
						CMS.Products.renderProduct($container,data);
					}
				}else{
					$container.append('<span sytle="color:red">与服务器通信发生未知错误，请稍候再试，谢谢！</span>');
				}
			});
		}
	});
	
    $('#keyword').on('keydown',function(e){
		if(e.keyCode==13){
			$('#search-button').click();
		}
	});
};
/*将服务器返回的json数据显示*/
CMS.Products.renderProduct = function($container,data){
	var products = data.products;

	if(products!==undefined){
		$container.empty();
		$.each(products,function(index,product){
			var imgUrl = product.photo;
			var brand = "";
			if(product.brand!=undefined){
				brand = '<span style="color:#999">'+product.brand.name+'</span>  <br>';
			}
			var product_name = brand + product.name + "("+product.model+")";
			var product_id = product.id;
			
			var data = {
					imgUrl : imgUrl,
					title : product_name,
					id : product_id
			};
			var product_template = template('product-template',data);
			$container.append(product_template);
		});
	}
};
CMS.Cases={};
CMS.Cases.gets = function(options,title){
	$(".section-title").text(title);
	$.get("cases_get.jspx?format=json",options,function(data){
		var $list_container = $("#case-list");
		var $show_container = $("#case-show");
		$list_container.empty();
		$show_container.empty();
		
		if(data!==undefined){
			var cases = data.cases;
			var show = data.show;
			if(show==="list"){
				if(cases!==undefined){
					$.each(cases,function(index,caseModel){
						var data = {
								"id":caseModel.id,
								"title":caseModel.title
						};
						$list_container.append(template('cases-template',data));
					});
				}
			}else if(show==="content"){
				var caseModel = data["case"];
				var data = {
						title:caseModel.title,
						addtime:caseModel.addtime,
						content:caseModel.content
				};
				console.debug(caseModel.content);
				$show_container.append(template('case-template',data));
			}else{
				$show_container.append(template('case-empty-template'));
			}
		}
	});
};
//检查图片是否存在   
function CheckImgExists(imgurl) {  
    var ImgObj = new Image(); //判断图片是否存在   
    ImgObj.src = imgurl;  
    //没有图片，则返回-1   
    if (ImgObj.fileSize > 0 || (ImgObj.width > 0 && ImgObj.height > 0)) {  
        return true;  
    } else {  
    	return false;
    }  
}  
